<?php 
	class CountorModel extends Model {
		public function __construct(){
			$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
		}
		public function increaseCountor(){
			$countorPage = $this->loadModel("CountorPage");
			$record = $this->findAsObject(array("REMOTE_ADDR="=>$_SERVER["REMOTE_ADDR"]));
			if($record===null) {
				// if client is not exist OR client is exist but he come to visit another day
				$record = $this->save(array(
						"HTTP_HOST"=>@$_SERVER["HTTP_HOST"],
						"HTTP_USER_AGENT"=>@$_SERVER["HTTP_USER_AGENT"],
						"HTTP_REFERER"=>@$_SERVER["HTTP_REFERER"],
						"SERVER_SOFTWARE"=>@$_SERVER["SERVER_SOFTWARE"],
						"SERVER_NAME"=>@$_SERVER["SERVER_NAME"],
						"SERVER_ADDR"=>@$_SERVER["SERVER_ADDR"],
						"SERVER_PORT"=>@$_SERVER["SERVER_PORT"],
						"REMOTE_ADDR"=>@$_SERVER["REMOTE_ADDR"],
						"SCRIPT_FILENAME"=>@$_SERVER["SCRIPT_FILENAME"],
						"REMOTE_PORT"=>@$_SERVER["REMOTE_PORT"],
						"GATEWAY_INTERFACE"=>@$_SERVER["GATEWAY_INTERFACE"],
						"SERVER_PROTOCOL"=>@$_SERVER["SERVER_PROTOCOL"],
						"REQUEST_METHOD"=>@$_SERVER["REQUEST_METHOD"],
						"QUERY_STRING"=>@$_SERVER["QUERY_STRING"],
						"REQUEST_URI"=>@$_SERVER["REQUEST_URI"],
						"SCRIPT_NAME"=>@$_SERVER["SCRIPT_NAME"],
						"PATH_INFO"=>@$_SERVER["PATH_INFO"],
						"PATH_TRANSLATED"=>@$_SERVER["PATH_TRANSLATED"],
						"PHP_SELF"=>@$_SERVER["PHP_SELF"],
						"REQUEST_TIME"=>@$_SERVER["REQUEST_TIME"],
						"countor"=>1,
						"created"=>date("Y-m-d H:i:s"),
						"modified"=>date("Y-m-d H:i:s")
				));
			}else if(is_object($record)){
				if(Basic::dateCompare(date("Y-m-d",strtotime($record->modified)), date("Y-m-d"))<0){
					// if client exist and he comes to visit the next day
					$record=$this->update(array("countor"=>($record->countor+1),"modified"=>date("Y-m-d H:i:s")),array("id="=>$record->id));
				}
				/*
				else if(Basic::dateCompare(date("Y-m-d",strtotime($record->modified)),date("Y-m-d"))===0){
					// if client exist and he comes to visit in the same day
					//$record=$this->update(array("HTTP_USER_AGENT"=>$user_agent),array("id="=>$record->id));
				}
				*/
			}
			$pages = $countorPage->pageFrequency($record->id,$_SERVER["REQUEST_URI"]);
			return $record ;
		}
		public function visitorsByDate($from="",$to=""){
			// get Countor by duration
			if(isset($from)&&is_date($from)){
				
			}
			$this->fetchObject("SELECT * FROM CountorPages WHERE created='$from' ");
		}
		public function countVisitorByDate(){
			return $this->countRows("SELECT countor_id, count(countor_id) AS visitor_frequency FROM CountorPages WHERE created LIKE '%".date("Y-m-d")."%' GROUP BY countor_id ");
		}
		public function pagesByDay($from="",$to=""){
			// get all pages and its frequency which user visited by date or duration
			$result = $this->query("SELECT Countors.id, Countors.REMOTE_ADDR , Countors.REQUEST_URI FROM Countors WHERE created LIKE '%".date("Y-m-d")."%' ;");
			$rows=array();
			while($row=mysql_fetch_object($result)){
				$result1 = $this->query("SELECT page , countor, HTTP_USER_AGENT FROM CountorPages WHERE countor_id=".$row->id);
				$rows1=array();
				while(($row1=mysql_fetch_object($result1))){
					$user_agent = explode("~",$row1->HTTP_USER_AGENT);
					$row1->HTTP_USER_AGENT = $user_agent ;
					$rows1[]=$row1;
				}
				$row->REQUEST_URI=$rows1;
				$rows[]=$row;
			}
			return $rows;
		}
		public function clickByDate($from="",$to=""){
			// get total of visiting times of the website by date or duration 
		}
		public function pageVisitedByDate($from="",$to=""){
			
		}
		public function pagesByIP($ip,$from="",$to=""){
			// get all pages and its frequency which user visited by date or duration
		}
	}